<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input style="width: 150px;" v-model="dataForm.activityName" placeholder="输入活动名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 150px;" v-model="dataForm.recipientName" placeholder="输入受助者姓名"
                  clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
            v-model="dataForm.time"
            type="daterange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.categoryId" placeholder="类别" clearable>
          <el-option
              v-for="item in categorys"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 150px;" v-model="dataForm.startAge" placeholder="开始年龄" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 150px;" v-model="dataForm.endAge" placeholder="结束年龄" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.nurseGrade" placeholder="护理类别" clearable>
          <el-option
              v-for="item in nurseType"
              :key="item.key"
              :label="item.value"
              :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.address" placeholder="输入居住地址" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button type="primary" @click="openDialog">输出成excel</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
      <el-table-column
          prop="activityName"
          header-align="center"
          align="center"
          label="活动名称">
      </el-table-column>
      <el-table-column
          prop="startTime"
          header-align="center"
          align="center"
          label="活动时间">
      </el-table-column>
      <el-table-column
          prop="fellowName"
          header-align="center"
          align="center"
          label="活动负责人">
      </el-table-column>
      <el-table-column
          prop="jointerName"
          header-align="center"
          align="center"
          label="对接人">
      </el-table-column>
      <el-table-column
          prop="recipientName"
          header-align="center"
          align="center"
          label="受助者姓名">
      </el-table-column>
      <el-table-column
          header-align="center"
          align="center"
          label="性别">
        <template v-slot:default="scope">
          {{ scope.row.gender === 0 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column
          prop="categoryName"
          header-align="center"
          align="center"
          label="类别">
      </el-table-column>
      <el-table-column
          header-align="center"
          align="center"
          label="护理类别">
        <template v-slot:default="scope">
          <span v-if="scope.row.nurseGrade===0">全自理</span>
          <span v-else-if="scope.row.nurseGrade===1">半失能</span>
          <span v-else-if="scope.row.nurseGrade===2">失能</span>
          <span v-else>正常</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="age"
          header-align="center"
          align="center"
          label="年龄">
      </el-table-column>
      <el-table-column
          prop="address"
          header-align="center"
          align="center"
          label="居住地址">
      </el-table-column>
      <el-table-column
          prop="detail"
          header-align="center"
          align="center"
          label="物质及帮助描述">
      </el-table-column>
      <el-table-column
          prop="detail"
          header-align="center"
          align="center"
          label="操作">
        <template v-slot:default="scope">
          <el-button v-if="scope.row.detailPic.length>0" type="primary" size="small" @click="handlePictureCardPreview(scope.row.detailPic)">查看图片</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <file-name-input v-if="dialogVisible" ref="fileNameInput" @export="exportList"></file-name-input>
    <ImageView
            :previewVisible="Previewshow" 
            :urlList=preVeiwUrlList 
            @update:previewVisible="previewVisible = $event"
            @close="closeImageViewer"
            />
  </div>
</template>

<script>
import service from "@/utils/httpRequest";
import fileNameInput from "@/views/modules/activityRecord/fileName-input.vue";
import ImageView from "@/components/ImageView.vue"; 
export default {
  components: {fileNameInput, ImageView},
  data() {
    return {
      categorys: [
        {
          id: 0,
          name: ''
        }
      ],
      
      nurseType: [
        {
          key: 0,
          value: '全自理'
        },
        {
          key: 1,
          value: '半失能'
        },
        {
          key: 2,
          value: '失能'
        },
        {
          key: 3,
          value: '正常'
        }
      ],
      dataForm: {
        time: [],
        startAge: '',
        endAge: '',
        activityName: '',
        recipientName: '',
        categoryId: '',
        nurseGrade: '',
        address: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      dialogVisible: false,
      Previewshow: false,
      preVeiwUrlList: []
    }
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    getCategorys() {
      service.get('/lovehelp/category/allcategory').then((response) => {
        if (response && response.code === 200) {
          this.categorys = response.categorys
        } else {
          this.categorys = []
        }
      }).catch((error) => {
        console.error(error)
      })
    },
    //打开对话框
    openDialog() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.fileNameInput.visible = true;
      })
    },
    // 获取数据列表
    getDataList() {
  this.getCategorys()
  this.dataListLoading = true
  service.get(`/lovehelp/activityrecord/list`, {
    params: {
      'page': this.pageIndex,
      'limit': this.pageSize,
      'activityName': this.dataForm.activityName,
      'categoryId': this.dataForm.categoryId,
      'startAge': this.dataForm.startAge,
      'endAge': this.dataForm.endAge,
      'startTime':this.dataForm.time? this.dataForm.time[0] : '',
      'endTime':this.dataForm.time? this.dataForm.time[1] : '',
      'nurseGrade': this.dataForm.nurseGrade,
      'address': this.dataForm.address,
      'isLove': this.dataForm.isLove,
      'isMaterial': this.dataForm.isMaterial,
      'recipientName': this.dataForm.recipientName,
    }
  }).then((response) => {
    if (response && response.code === 200) {
      // 这里添加过滤逻辑
      this.dataList = response.page.list;
      //设置图片预览
      this.dataList.forEach((item, index) => {
        if (item.detailPic) {
          item.detailPic = item.detailPic.split(',');
        }else{
          item.detailPic = [];
        }
      })
      this.totalPage = response.page.totalCount;
    } else {
      this.dataList = [];
      this.totalPage = 0;
    }
    this.dataListLoading = false;
  }).catch((error) => {
    // 处理错误
    console.error(error);
  });
},
    exportList(fileName){
      service({
        method: 'POST',
        url: '/lovehelp/activityrecord/exportrecord',
        params: {
          'fileName': fileName,
          'name': this.dataForm.name,
          'categoryId': this.dataForm.categoryId,
          'startAge': this.dataForm.startAge,
          'endAge': this.dataForm.endAge,
          'startTime':this.dataForm.time? this.dataForm.time[0] : '',
          'endTime':this.dataForm.time? this.dataForm.time[1] : '',
          'nurseGrade': this.dataForm.nurseGrade,
          'address': this.dataForm.address,
          'isLove': this.dataForm.isLove,
          'isMaterial': this.dataForm.isMaterial,
          'recipientName': this.dataForm.recipientName,
        }
      }).then(res => {
        if (res.code === 200) {
          this.$message.success("导出成功");
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    handlePictureCardPreview(file) {
      this.preVeiwUrlList = file
      // this.preVeiwUrlList.push(file.url)
      this.Previewshow = true
    },
    closeImageViewer() {
      this.Previewshow = false
    },
  }
}
</script>
